package edu.ptu.communityplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.ptu.communityplatform.common.controller.AjaxResponse;
import edu.ptu.communityplatform.common.controller.BaseController;
import edu.ptu.communityplatform.common.controller.TableInfo;
import edu.ptu.communityplatform.domain.entity.ParkingLot;
import edu.ptu.communityplatform.domain.queryParam.parkingLotParam.ParkingAddParam;
import edu.ptu.communityplatform.domain.queryParam.parkingLotParam.ParkingUpdateParam;
import edu.ptu.communityplatform.domain.vos.ParkingPageVo;
import edu.ptu.communityplatform.domain.vos.ParkingVo;
import edu.ptu.communityplatform.service.IParkingLotService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import static edu.ptu.communityplatform.mapping.ParkingLotMapping.PARKING_MAPPING;

/**
 * @BelongsProject: communityPlatform
 * @BelongsPackage: edu.ptu.communityplatform.controller
 * @Author: HYJ
 * @CreateTime: 2025-04-08  19:17
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/parking")
public class ParkingLotController extends BaseController {

    @Resource
    private IParkingLotService parkingLotService;

    /**
     * 根据ID查询停车位信息
     *
     * @param parkingId 停车位ID
     * @return 停车位信息
     */
    @GetMapping("/{parkingId}")
    public AjaxResponse getDtlById(@PathVariable Long parkingId) {

        ParkingLot parkingLot = parkingLotService.getById(parkingId);
        ParkingVo parkingVo = PARKING_MAPPING.parkingToParkingVo(parkingLot);

        return parkingVo != null ? AjaxResponse.success(parkingVo) : AjaxResponse.fail("fail");
    }

    /**
     * 分页查询停车位信息
     *
     * @param pageNum
     * @param pageSize
     * @param plotId
     * @param parkingName
     * @param type
     * @return
     */
    @GetMapping("/list")
    public TableInfo list(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = true, defaultValue = "5") Integer pageSize,
                          @RequestParam(required = true) Long plotId,
                          @RequestParam(required = false) String parkingName,
                          @RequestParam(required = false) Integer type
    ) {


        IPage<ParkingLot> parkingPage = parkingLotService.getParkingLotPage(pageNum, pageSize, plotId, parkingName, type);

        // 将分页结果转换为VO对象列表
        // 使用PARKING_MAPPING进行实体到VO的字段映射
        List<ParkingPageVo> pageVos = parkingPage.convert(PARKING_MAPPING::parkingToParkingPageVO).getRecords();

        return toJson(parkingPage != null, pageVos, pageVos.size());
    }

    /**
     * 添加停车位信息
     *
     * @param param 停车位信息
     * @return 新增结果
     */
    @PostMapping()
    public AjaxResponse addParkingLot(@RequestBody ParkingAddParam param) {
        //校验参数
        String parkingName = param.getParkingName();
        Integer type = param.getType();

        if (parkingName == null || Objects.equals("", parkingName)) {
            return AjaxResponse.fail("停车场名称不能为空");
        }

        if (type == null) {
            return AjaxResponse.fail("停车位类型不能为空");
        }


        ParkingLot parkingLot = new ParkingLot();

        // 设置属性
        parkingLot.setParkingName(parkingName);
        parkingLot.setPlotId(param.getPlotId());
        parkingLot.setType(type);

        // 调用服务层方法进行保存
        boolean save = parkingLotService.save(parkingLot);
        return save ? AjaxResponse.success("success") : AjaxResponse.fail("fail");
    }

    /**
     * 修改停车位信息
     *
     * @param param
     * @return
     */
    @PutMapping()
    public AjaxResponse updateParkingLot(@RequestBody ParkingUpdateParam param) {

        //校验参数
        String parkingName = param.getParkingName();
        if (parkingName == null || Objects.equals("", parkingName)) {
            return AjaxResponse.fail("停车场名称不能为空");
        }

        //构建更新条件
        LambdaQueryWrapper<ParkingLot> wrapper = new LambdaQueryWrapper<>();

        //设置更新条件
        wrapper.eq(ParkingLot::getParkingId, param.getParkingId());

        ParkingLot parkingLot = new ParkingLot();
        //设置更新属性
        parkingLot.setParkingName(parkingName);
        parkingLot.setType(param.getType());

        boolean update = parkingLotService.update(parkingLot, wrapper);

        return update ? AjaxResponse.success("success") : AjaxResponse.fail("fail");
    }

    /**
     * 删除停车位信息
     *
     * @param parkingId
     * @return
     */
    @Transactional
    @DeleteMapping("/{parkingId}")
    public AjaxResponse deleteParkingLot(@PathVariable Long parkingId) {
        //构建更新条件
        LambdaQueryWrapper<ParkingLot> wrapper = new LambdaQueryWrapper<>();

        //设置更新条件
        wrapper.eq(ParkingLot::getParkingId, parkingId);

        boolean remove = parkingLotService.remove(wrapper);
        return remove ? AjaxResponse.success("success") : AjaxResponse.fail("fail");
    }
}
